//
//  MHSettingCirleViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/20.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSettingCirleViewController.h"
#import "MHSettingCircleViewModel.h"
#import "MHAlertMessageView.h"

@interface MHSettingCirleViewController ()
@property (nonatomic, readonly, strong) MHSettingCircleViewModel *viewModel;
@end

@implementation MHSettingCirleViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setupSubViews];
    self.tableView.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
    self.view.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
}

#pragma mark - Override
- (void)bindViewModel{
    [super bindViewModel];
    @weakify(self);
    [[self.viewModel.exitCircle.executing
      skip:1]
     subscribeNext:^(NSNumber * showHud) {
         @strongify(self);
         if (showHud.boolValue) {
             [MBProgressHUD mh_showProgressHUD:@"正在退出..."];
         }else{
             [MBProgressHUD mh_hideHUD];
         }
     }];
   
    
}

- (void)_setupSubViews{
    UIButton *footerView = [UIButton buttonWithType:UIButtonTypeCustom];
    footerView.mh_height = 60.f;
    [footerView setTitle:@"退出圈子" forState:UIControlStateNormal];
    [footerView setTitleColor:MH_MAIN_TEXTCOLOR66 forState:UIControlStateNormal];
    footerView.titleLabel.font = MH_MAIN_TEXTFONT_20;
    
    [footerView setBackgroundImage:[UIImage yy_imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [footerView setBackgroundImage:[UIImage yy_imageWithColor:MHColor(217, 217, 217)] forState:UIControlStateHighlighted];
    self.tableView.tableFooterView = footerView;
    
    /// 事件处理
    @weakify(self);
    [[footerView rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(UIButton *sender) {
         @strongify(self);
         self.zh_popupController = [zhPopupController new];
         BOOL isOwner = [self.viewModel.circleInfo currentUserIsOwner];
         MHAlertMessageView *alert = [[MHAlertMessageView alloc] initWihtTitle:@"退出圈子" message:(isOwner ?  @"圈主退出后，第一位管理员或者第一位成员将成为圈主":@"退出后将收不到圈子内的精彩内容了哦~")];
         
         alert.mh_width = self.view.mh_width * 0.8;
         __weak __typeof(self)weakSelf = self;
         [alert.dissMissButton bk_whenTapped:^{
             [weakSelf.zh_popupController dismiss];
         }];
         [alert.comfirmButton bk_whenTapped:^{
             [[weakSelf.viewModel.exitCircle execute:nil]subscribeNext:^(id x) {
                [MHNotificationCenter postNotificationName:MHReloadFirstPageDataNotification object:nil];
                  [MHNotificationCenter postNotificationName:MHReloadHomeDiscoverDataNotification object:nil];
             }];
             [weakSelf.zh_popupController dismiss];
         }];
         [self.zh_popupController presentContentView:alert duration:0.25 springAnimated:YES];
     }];
}


@end
